package com.ioc.business.order.controller;


import cn.hutool.core.util.ObjectUtil;
import com.ioc.business.order.domain.DeliveryOrder;
import com.ioc.business.order.domain.bo.delivery.DeliveryOrderBo;
import com.ioc.business.order.domain.bo.delivery.DeliveryOrderQueryBo;
import com.ioc.business.order.service.IDeliveryOrderService;
import com.ioc.business.order.vo.delivery.DeliveryOrderImportVo;
import com.ioc.business.order.vo.delivery.DeliveryOrderPageVo;
import com.ioc.business.order.vo.delivery.DeliveryOrderVo;
import com.ioc.common.annotation.Log;
import com.ioc.common.core.controller.BaseController;
import com.ioc.common.core.domain.AjaxResult;
import com.ioc.common.core.page.TableDataInfo;
import com.ioc.common.core.page.TableVo;
import com.ioc.common.enums.BusinessType;
import com.ioc.common.utils.poi.ExcelUtil;
import com.ioc.common.validate.AddGroup;
import com.ioc.common.validate.EditGroup;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 * 发货订单表 前端控制器
 * </p>
 *
 * @author WeiZhongRong
 * @since 2025-01-07
 */
@RestController
@RequestMapping("/delivery-order")
@AllArgsConstructor
public class DeliveryOrderController extends BaseController {

    private final IDeliveryOrderService deliveryOrderService;

    /**
     * 分页查询发货订单列表
     *
     * @param queryBo
     * @return
     * @folder 发货订单/分页查询发货订单列表
     */
    @GetMapping("/list")
    public TableDataInfo selectDeliveryOrderPageList(DeliveryOrderQueryBo queryBo) {
        startPage();
        TableVo<DeliveryOrder, DeliveryOrderPageVo> data = deliveryOrderService.selectDeliveryOrderPageList(queryBo);
        return getDataTable(data);
    }

    /**
     * 查询发货订单详情信息
     *
     * @param deliveryOrderId
     * @return
     * @folder 发货订单/查询发货订单详情信息
     */
    @GetMapping("/{deliveryOrderId}")
    public AjaxResult getDeliveryOrderInfo(@PathVariable("deliveryOrderId") Long deliveryOrderId) {
        DeliveryOrderVo deliveryOrderVo = deliveryOrderService.selectDeliveryOrderById(deliveryOrderId);
        if (ObjectUtil.isEmpty(deliveryOrderVo)) {
            return error("数据不存在!");
        }
        return success(deliveryOrderVo);
    }

    /**
     * 发货订单
     *
     * @param bo
     * @return
     * @folder 发货订单/新增发货订单
     */
    @Log(title = "发货订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult addDeliveryOrder(@RequestBody @Validated(AddGroup.class) DeliveryOrderBo bo) {
        return toAjax(deliveryOrderService.addDeliveryOrder(bo));
    }

    /**
     * 编辑发货订单
     *
     * @param bo
     * @return
     * @folder 发货订单/编辑发货订单
     */
    @PostMapping("/edit")
    @Log(title = "发货订单", businessType = BusinessType.UPDATE)
    public AjaxResult updateDeliveryOrder(@RequestBody @Validated(EditGroup.class) DeliveryOrderBo bo) {

        return toAjax(deliveryOrderService.updateDeliveryOrder(bo));
    }

    /**
     * 删除发货订单
     *
     * @param idList
     * @return
     * @folder 发货订单/删除发货订单
     */
    @Log(title = "发货订单", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    public AjaxResult remove(@RequestBody List<Long> idList) {
        return toAjax(deliveryOrderService.removeDeliveryOrder(idList));
    }

    /**
     * 下载发货订单模版
     *
     * @param response
     * @folder 发货订单/下载发货订单模版
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<DeliveryOrderImportVo> util = new ExcelUtil<>(DeliveryOrderImportVo.class);
        util.importTemplateExcel(response, "发货订单导入模版");
    }

    /**
     * 导入发货订单数据
     *
     * @param file
     * @return
     * @throws Exception
     * @folder 发货订单/导入发货订单数据
     */
    @Log(title = "收货订单", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<DeliveryOrderImportVo> util = new ExcelUtil<>(DeliveryOrderImportVo.class);
        List<DeliveryOrderImportVo> voList = util.importExcel(file.getInputStream());
        deliveryOrderService.importDeliveryOrder(voList);
        return success();
    }

}
